﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using DispatchAssist.Service;
using System.Web.Services.Protocols;

namespace DispatchAssist.AppHost
{
    [ModuleContract("96ca3f13-5e36-4c64-8de5-982c4bb361a4", "系统管理", "修改密码")]
    public partial class ChangePassword : MdiUserControl
    {
        AuthService authService = new AuthService();

        public ChangePassword()
            : base()
        {
            InitializeComponent();

        }
        public ChangePassword(XmlNode node)
            : this()
        {
        }
        public override void Onload()
        {
            base.Onload();
            authService.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            lblUserName.Text = AuthContext.Current.User.UserName;

            txtOldPwd.Text = "";
            txtNewPwd.Text = "";
            txtConfirmPwd.Text = "";
            txtOldPwd.Focus();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                if (string.IsNullOrEmpty(this.txtOldPwd.Text))
                {
                    MessageBox.Show("请输入旧密码！");
                    return;
                }
                if (string.IsNullOrEmpty(this.txtNewPwd.Text))
                {
                    MessageBox.Show("请输入新密码！");
                    return;
                }
                if (string.IsNullOrEmpty(this.txtConfirmPwd.Text))
                {
                    MessageBox.Show("请输入确认密码！");
                    return;
                }
                if (this.txtNewPwd.Text != this.txtConfirmPwd.Text)
                {
                    MessageBox.Show("两次密码输入不一致！");
                    return;
                }
                SeatUser user = new SeatUser();
                user.UserName = AuthContext.Current.User.UserName;
                user.UserID = AuthContext.Current.User.UserID;
                user.Password = CarolLib.EncryptHelper.GetPassword(this.txtNewPwd.Text);
                DataAccessReturnValue result = authService.ChangePassword(user, CarolLib.EncryptHelper.GetPassword(this.txtOldPwd.Text));
                if (result == DataAccessReturnValue.无法更新)
                {
                    MessageBox.Show("密码错误！");
                }
                else if (result == DataAccessReturnValue.更新成功)
                {
                    MessageBox.Show("密码修改成功！");
                    txtOldPwd.Text = "";
                    txtNewPwd.Text = "";
                    txtConfirmPwd.Text = "";
                    txtOldPwd.Focus();

                }
                else
                {
                    MessageBox.Show("密码修改失败！");
                }
            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtConfirmPwd.Text = string.Empty;
            this.txtNewPwd.Text = string.Empty;
            this.txtOldPwd.Text = string.Empty;
            txtOldPwd.Focus();

        }
    }
}
